<template>
  <div style="">
    <Header :left="true" leftText="返回" title="停机维护"> </Header>
    <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold"
      left-icon="description">查询条件</NoticeBar>
    <Form :style="autoHeight">
      <van-cell title="日期选择" class="formstyle" required placeholder="请选择日期" is-link :value="form.planworkdatetime"
        @click="showdate = true" />
      <van-calendar :min-date="minDate" color="#1989fa" v-model="showdate" @confirm="onConfirm" />
      <!--<van-field
        required
        readonly
        class="formstyle"
        name="picker"
        :value="valueshiftid"
        label="班次选择"
        placeholder="点击选择班次"
        @click="showPickershiftid = true"
      />
      <van-popup v-model="showPickershiftid" position="bottom">
        <van-picker
          show-toolbar
          :columns="columnsshift"
          @confirm="onConfirmshiftid"
          @cancel="showPickershiftid = false"
        >
          <template #option="option">
            <div style="display: flex; flex-direction: column; align-items: center;">
              <div>{{ option.pshiftname }}</div>
            </div>
          </template>
        </van-picker>
      </van-popup>-->

      <van-field required readonly class="formstyle" name="picker" :value="valuepline" label="生产线选择" placeholder="点击选择产线"
        @click="showPickerpline = true" />
      <van-popup v-model="showPickerpline" position="bottom">
        <van-picker show-toolbar :columns="columnspline" @confirm="onConfirmpline" @cancel="showPickerpline = false">
          <template #option="option">
            <div style="display: flex; flex-direction: column; align-items: center;">
              <div>{{ option.plineName }}</div>
            </div>
          </template>
        </van-picker>
      </van-popup>
    </Form>

    <div style="margin-top: 10px; margin-bottom: 10px">
      <van-button type="primary" class="buttongroup" @click="operatedetail" color="#138087">查询</van-button>
    </div>

    <!-- <div class="textScroll"> -->
    <div v-for="(item) in items" :key="item.id">
      <van-cell icon="circle" style="font-weight: bold;font-size:17px"
        :value="`【机台名称】:${item.MachineID} 【开始停机时间】:${item.StartDateTime} 【结束停机时间】:${item.EndDateTime} 【停机时长】:${item.TimeLong}(H)`"
        @click="modifydata(item.MachineID, item.StartDateTime, item.EndDateTime, item.MachineMainStatus, item.MachineProductionStatus, item.TimeLong, item.FactoryID, item.Id)" />
    </div>
    <!-- </div> -->
  </div>
</template>

<script>
import {
  Col,
  Row,
  Form,
  Cell,
  Icon,
  Field,
  NumberKeyboard,
  Image as VanImage,
  Picker,
  popup,
  SubmitBar,
  Button,
  Loading,
  Overlay,
  Dialog,
  Toast,
  NoticeBar,
  Tag,
  DatetimePicker,
  ImagePreview,
  CellGroup,
  Uploader,
  Cascader,
  Calendar,
  RadioGroup,
  Radio,
  Divider,
  Stepper,
  AddressList,
  Checkbox,
  CheckboxGroup,
} from "vant";

import "vue-easytable/libs/themes-base/index.css";
import { VTable } from "vue-easytable";
import { _httpClient } from "@/libs/apisie";
import { _httpClient as _httpClientApi } from "@/libs/api";
import { getToken, getBaseApiFrefix } from "@/libs/auth";
import Header from "@/components/header.vue";
import { storage } from "@/libs/utils";

Vue.use(Field);
Vue.use(Overlay);
Vue.use(Loading);
Vue.use(Picker);
Vue.use(popup);
Vue.use(SubmitBar);
Vue.use(VanImage);
Vue.use(DatetimePicker);
Vue.use(Cascader);
Vue.use(Calendar);
Vue.use(Cell);
Vue.use(RadioGroup);
Vue.use(Radio);
Vue.use(Divider);
Vue.use(Stepper);
Vue.use(Button);
Vue.use(AddressList);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(Col);
Vue.use(Row);

export default {
  name: "downtime",
  components: {
    Row,
    Col,
    Button,
    Header,
    Form,
    Cell,
    Icon,
    Field,
    NumberKeyboard,
    Dialog,
    Toast,
    NoticeBar,
    Row,
    Tag,
    popup,
    DatetimePicker,
    ImagePreview,
    CellGroup,
    Uploader,
    Col,
    Calendar,
    Cascader,
    Picker,
    RadioGroup,
    Radio,
    Divider,
    Stepper,
    VTable,
    AddressList,
    Checkbox,
    CheckboxGroup,
  },
  data() {
    return {
      columnspline: [],
      showPickerpline: false,
      showPickershiftid: false,
      result: [],
      items: [
      ], //查询数据项
      columnsshift: [
      ],
      valueshiftid: "",
      date: "",
      showdate: false,
      companyname: "",
      minDate: new Date(2021, 12, 1),
      form: {
        companyid: "",
        planworkdatetime: "",
        shiftid: "",
        plineid: "",
      },
      valuepline: "",
      user: []
    };
  },

  created: function () {
    this.user = JSON.parse(storage.get("user"));
    this.getFactoryID();
    var myDate = new Date();
    this.form.planworkdatetime =
      myDate.getFullYear() +
      "-" +
      (myDate.getMonth() + 1) +
      "-" +
      (myDate.getDate());
    //获取部门明细
    this.companyname = this.user.dept.deptName;
    this.form.companyid = this.user.dept.deptId;
    /* this.getshift(); */
  },
  mounted() {
  },
  methods: {
    //获取工厂信息
    async getFactoryID() {
      let _this = this;
      try {
        let res = await _httpClientApi.get(
          _httpClientApi.getApiPrefix() + '/system/ConvertFactoryID/getList/' + _this.user.site,
        );
        if (res.code === 200) {
          _this.user.site = res.data.sapfactoryid;
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
      this.getPline();
    },
    onConfirmpline(value, index) {
      this.form.plineid = this.columnspline[index].plineId;
      this.valuepline = this.columnspline[index].plineName;
      this.showPickerpline = false;
    },
    /*  onConfirmshiftid(value,index) {
       this.valueshiftid = this.columnsshift[index].pshiftname;
       this.form.shiftid = this.columnsshift[index].pShiftID;
       this.showPickershiftid = false;
     }, */
    //根据⽣产线代码获取停机明细
    async operatedetail() {
      let _this = this;
      if (_this.form.planworkdatetime == "") {
        Toast.fail("请选择日期");
        return;
      }
      if (_this.form.plineid == "") {
        Toast.fail("请选择机台");
        return;
      }
      /*  if (_this.form.shiftid == "") {
        Toast.fail("请选择班次");
        return;
      } */
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "请稍等...",
      });
      try {
        let res = await _httpClient.get(
          _httpClient.getApiPrefix() + "/MachineRunStopTime/GetList?FactoryID=" + _this.user.site + "&MachineID=" + _this.form.plineid + "&StartDateTimeBegin=" + _this.form.planworkdatetime,
        );
        if (res.Data.length == 0) {
          Toast.clear();
          Toast.fail("未查找到相关数据");
        } else {
          Toast.clear();
          Toast.success("查询完成");
          _this.items = res.Data;
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    createdata() {
      this.$ac.openWin({
        name: "manhourfact-temporarywork",
        url: "./manhourfact-temporarywork.html",
      });
    },
    modifydata(val1, val2, val3, val4, val5, val6, val7, val8) {
      let _this = this;
      this.$ac.openWin({
        name: "equipmentsj-downtimeitem",
        url: "./equipmentsj-downtimeitem.html",
        pageParam: {
          plineid: val1,
          startdatetime: val2,
          endDateTime: val3,
          MachineMainStatus: val4,
          MachineProductionStatus: val5,
          TimeLong: val6,
          factoryID: val7,
          Id: val8
        }
      });
    },
    formatDate(date) {
      return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    },
    onConfirm(date) {
      this.showdate = false;
      this.form.planworkdatetime = this.formatDate(date);
    },
    //根据⼯⼚代码获取机台信息
    //获取生产线
    async getPline() {
      let _this = this;
      try {
        let res = await _httpClient.get(
          _httpClient.getApiPrefix() + "/PLineEquipment/GetList?FactoryID=TC00",
        );
        if (res.HttpCode === "200") {
          let teamMap = res.Data.map(item => ({ plineId: item.PLineID, plineName: item.PLineName }))
          _this.columnspline = teamMap
          _this.columnsplinenew = teamMap
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    //获取班次信息
    /*  async getshift(){
       let _this = this;
       try {
         let res = await _httpClient.get(
           _httpClient.getApiPrefix() + "/ProductionShift/GetList?FactoryID=" + _this.user.site,
         );
         if (res.HttpCode === "200") {        
           let shiftMap = res.Data.map(item =>({pshiftname: item.PShiftName, pShiftID: item.PShiftID, pShiftTime:item.ShiftLong}))
           _this.columnsshift = shiftMap
         }
       } catch (e) {
         Toast.fail(JSON.stringify(e));
       }
     }, */
  },
};
</script>

<style>
body {
  background-color: white;
}

.table-title-class {
  background-color: #304156;
  color: #fff;
}

.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.buttongroup {
  float: right;
  width: 30%;
  margin-right: 10px;
  margin-top: 10px;
  margin-bottom: 20px;
}

.container.dwo {
  display: flex;
  justify-content: center;
  align-items: center;
}

.buttonLeft {
  position: fixed;
  bottom: 8px;
  float: left;
  width: 49%;
}

.formstyle {
  font-size: 15px;
  font-weight: bold;
  display: flex;
  align-items: center;
}

.radioteam {
  display: flex;
  align-items: center;
  padding-left: 13px;
  margin-top: 10px;
  margin-left: 34px;
  margin-bottom: 10px;
}

.buttonRight {
  position: fixed;
  bottom: 8px;
  float: right;
  left: 51%;
  width: 49%;
  background-color: #f22028;
  border-color: #f22028;
}

.textScroll {
  display: inline-block;
  position: fixed;
  top: 45%;
  bottom: 3rem;
  overflow: scroll;
  right: 10px;
  width: 100%;
  z-index: 1;
}

.message {
  font-size: 15px;
  margin-top: 5px;
  margin-left: -10px;
  font-weight: bold;
}

.mg5 {
  margin-right: 5px;
}

.tag_margin {
  margin: 2px 5px;
}

.card_under {
  height: 100%;
  width: 100%;
  margin: auto;
  background: #ffffff;
  border-radius: 25px;
  box-shadow: 0 0px 0px rgba(0, 0, 0, 0.3);
}

.card_icon {
  text-align: center;
  /* position: absolute; */
  height: 8rem;
  width: 80%;
  margin: 12% 10%;
  background: rgba(35, 180, 237, 1);
  border-radius: 25px;
  box-shadow: 0 10px 50px rgba(0, 0, 0, 0.3);
}

.text {
  font-size: 18px;
  color: #4e4a67;
  margin: 8px auto auto 20px;
  line-height: 1.5rem;
  text-align: justify;
  font-weight: 600;
}

.text_small {
  font-size: 16px;
  color: red;
  margin: 8px auto auto 20px;
  line-height: 1.5rem;
  text-align: justify;
  font-weight: 700;
}

.text_scan {
  font-size: 16px;
  color: #4e4a67;
  margin: auto auto auto 20px;
  line-height: 1.5em;
  text-align: justify;
}

.divStyle {
  margin-left: 3px;
  margin-right: 3px;
}
</style>
